



import java.util.ArrayList;

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.*;
import org.apache.commons.httpclient.methods.*; 
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/** 
 * ÓÃÀ´ÑÝÊ¾µÇÂ¼±íµ¥µÄÊ¾Àý 
 * @author Liudong 
 */
public class GradesCatcher {
   static String LOGON_SITE = "jwc.njust.edu.cn" ;
   static int     LOGON_PORT = 6666;
   static String stu_id="1107830141";
   static String stu_pwd="920507";
   public static String getLOGON_SITE() {
        return LOGON_SITE;
}

   public static void setLOGON_SITE(String lOGON_SITE) {
        LOGON_SITE = lOGON_SITE;
}

   public static int getLOGON_PORT() {
        return LOGON_PORT;
}

   public static void setLOGON_PORT(int lOGON_PORT) {
        LOGON_PORT = lOGON_PORT;
}

   public static String getStu_id() {
        return stu_id;
}

   public static void setStu_id(String stu_id) {
        GradesCatcher.stu_id = stu_id;
}

   public static String getStu_pwd() {
        return stu_pwd;
}

   public static void setStu_pwd(String stu_pwd) {
        GradesCatcher.stu_pwd = stu_pwd;
}

public static String getHtml() throws Exception{
      HttpClient client = new HttpClient();
      client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT);

      // Ä£ÄâµÇÂ¼Ò³Ãæ login.jsp->main.jsp
      PostMethod post = new PostMethod( "/pls/wwwbks/bkscjcx.login" );
      NameValuePair name = new NameValuePair( "stuid" , stu_id );
      NameValuePair pass = new NameValuePair( "pwd" , stu_pwd );
      post.setRequestBody( new NameValuePair[]{name,pass});
      int status = client.executeMethod(post);
      System.out.println(post.getResponseBodyAsString());
      post.releaseConnection();

      // ²é¿´ cookie ÐÅÏ¢
      CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
      Cookie[] cookies = cookiespec.match(LOGON_SITE, LOGON_PORT, "/" , false , client.getState().getCookies());
      if (cookies.length == 0) {
         System.out.println( "None" );
      } else {
         for ( int i = 0; i < cookies.length; i++) {
            System.out.println(cookies[i].toString());
         }
      }

      // ·ÃÎÊËùÐèµÄÒ³Ãæ 
      GetMethod get=new GetMethod("/pls/wwwbks/bkscjcx.tsjykcx");
      client.executeMethod(get);
      String html=get.getResponseBodyAsString();
      get.releaseConnection();
      return html;
   }
   public ArrayList parseHtml() throws Exception{ 
           ArrayList<String[]> list=new ArrayList<String[]>();
           String html=getHtml();
           Document doc = Jsoup.parse(html);
           //»ñÈ¡³É¼¨ÁÐ±íÖÐµÄÃ¿Ò»ÐÐ,°üÀ¨±íÍ·ÐÐ
           Elements rowItems=doc.select("tbody tr");
           //»ñÈ¡³É¼¨±íÖÐ±íÍ·ÐÅÏ¢£¬È»ºóÉ¾³ýµô±íÍ·ÐÐ
           Elements thItems=rowItems.get(0).select("th");
           String[] strs=new String[8];
           for(int i=0;i<8;i++){ 
                   strs[i]=thItems.get(i).text();
           }
           //²»ÄÜÖ±½ÓÌí¼ÓÊý×éÃû£¬²»È»»á¸²¸ÇÖ®ºóÌí¼ÓµÄ±äÁ¿¡£
           list.add(new String[]{strs[0],
                           strs[1],
                           strs[2],
                           strs[3],
                           strs[4],
                           strs[5],
                           strs[6],
                           strs[7],
                          });
           rowItems.remove(0);
           //½«³É¼¨ÁÐ±íÖÐµÄ¿Î³Ì³É¼¨ÐÅÏ¢½âÎöµ½listÁÐ±íÖÐ
           for( Element rowItem : rowItems){ 
                   Elements colItems=rowItem.select("td");
                   for(int i=0;i<8;i++){ 
                           strs[i]=colItems.get(i).text();
                   }
                   list.add(new String[]{strs[0],
                                   strs[1],
                                   strs[2],
                                   strs[3],
                                   strs[4],
                                   strs[5],
                                   strs[6],
                                   strs[7],
                                 });
           }
           //listÁÐ±íÖÐµÚÒ»¸öÊý×éÔªËØÎª±íÍ·ÐÅÏ¢£¬Ö®ºóµÄÔªËØÎª¶ÔÓ¦±íÍ·ÐÅÏ¢µÄ¿Î³ÌÐÅÏ¢
           return list;
   }
   public void main(String[] args) throws Exception{ 
           ArrayList arraylist=this.parseHtml();
           for(int i=0;i<arraylist.size();i++){
                   String[] strs=(String[])arraylist.get(i);
                   for(int j=0;j<8;j++){
                   System.out.print(" "+strs[j]);
                   }
                   System.out.println();
           }
   }
}